/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.com.procesos;

import pe.com.unidades.Telefono;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.IOUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.message.BasicNameValuePair;

/**
 *
 * @author romulogalindo
 */
public class ProcesadorTelefonoRPM extends Proceso{

    private Telefono cel;

    public ProcesadorTelefonoRPM(Telefono cel) {
        this.cel = cel;
    }

    public Telefono getCel() {
        return cel;
    }

    public void setCel(Telefono cel) {
        this.cel = cel;
    }

    public String conseguirRPM() {
        List loginNvps = new ArrayList();
        List loginNvps2 = new ArrayList();
        String res;
        org.apache.http.HttpResponse resp = null;
        org.apache.http.impl.client.DefaultHttpClient client = new org.apache.http.impl.client.DefaultHttpClient();
        HttpGet http = new HttpGet("http://200.60.223.134/vircanfiext/DirectorioRpm/frmDirectorioRpmRedi.aspx");
        try {
            resp = client.execute(http);
            org.apache.http.HttpEntity entity = resp.getEntity();
            InputStream instream = null;
            instream = entity.getContent();
            String rpta1 = IOUtils.toString(instream, "UTF-8");

            rpta1 = rpta1.substring(rpta1.indexOf("__VIEWSTATE") + 12, rpta1.length());
            rpta1 = rpta1.substring(rpta1.indexOf("value") + 7, rpta1.length());
            String __VIEWSTATE = rpta1.substring(0, rpta1.indexOf("\""));

            rpta1 = rpta1.substring(rpta1.indexOf("__EVENTVALIDATION") + 18, rpta1.length());
            rpta1 = rpta1.substring(rpta1.indexOf("value") + 7, rpta1.length());
            String __EVENTVALIDATION = rpta1.substring(0, rpta1.indexOf("\""));
//            System.out.println("__VIEWSTATE:" + __VIEWSTATE);
//            System.out.println("__EVENTVALIDATION:" + __EVENTVALIDATION);
            instream.close();
            List<Cookie> lcookies = client.getCookieStore().getCookies();
            for (Cookie c : lcookies) {
                System.out.println("[" + c.getName() + ":" + c.getValue() + "]");
            }

            /**
             *
             */
            NameValuePair p_2 = new BasicNameValuePair("__EVENTTARGET", "ddlTipoConsulta");
            NameValuePair p_1 = new BasicNameValuePair("__LASTFOCUS", "");
            NameValuePair p_3 = new BasicNameValuePair("__EVENTARGUMENT", "");
            NameValuePair p_4 = new BasicNameValuePair("__VIEWSTATE", __VIEWSTATE);
            NameValuePair p_5 = new BasicNameValuePair("__EVENTVALIDATION", __EVENTVALIDATION);
            NameValuePair p_6 = new BasicNameValuePair("ddlTipoConsulta", "1");
            NameValuePair p_7 = new BasicNameValuePair("txtNro", cel.getNumero());
            loginNvps.add(p_1);
//            loginNvps.add(p_2);
            loginNvps.add(p_3);
            loginNvps.add(p_4);
            loginNvps.add(p_5);
            loginNvps.add(p_6);
//            loginNvps.add(p_7);
            /**
             *
             */
            HttpPost http2 = new HttpPost("http://200.60.223.134/vircanfiext/DirectorioRpm/frmDirectorioRpmRedi.aspx");
            http2.setEntity(new UrlEncodedFormEntity(loginNvps, "UTF-8"));
            org.apache.http.HttpResponse resp2 = client.execute(http2);
            org.apache.http.HttpEntity entity2 = resp2.getEntity();
            InputStream instream2 = null;
            instream2 = entity2.getContent();
            String rpta2 = IOUtils.toString(instream2, "UTF-8");

            rpta2 = rpta2.substring(rpta2.indexOf("__VIEWSTATE") + 12, rpta2.length());
            rpta2 = rpta2.substring(rpta2.indexOf("value") + 7, rpta2.length());
            String __VIEWSTATE2 = rpta2.substring(0, rpta2.indexOf("\""));

            rpta2 = rpta2.substring(rpta2.indexOf("__EVENTVALIDATION") + 18, rpta2.length());
            rpta2 = rpta2.substring(rpta2.indexOf("value") + 7, rpta2.length());
            String __EVENTVALIDATION2 = rpta2.substring(0, rpta2.indexOf("\""));
//            System.out.println("__VIEWSTATE2:" + __VIEWSTATE2);
//            System.out.println("__EVENTVALIDATION2:" + __EVENTVALIDATION2);
            instream2.close();
            /**
             *
             */
            NameValuePair p_21 = new BasicNameValuePair("__EVENTTARGET", "imbEnviar");
            NameValuePair p_11 = new BasicNameValuePair("__LASTFOCUS", "");
            NameValuePair p_31 = new BasicNameValuePair("__EVENTARGUMENT", "");
            NameValuePair p_41 = new BasicNameValuePair("__VIEWSTATE", __VIEWSTATE2);
            NameValuePair p_51 = new BasicNameValuePair("__EVENTVALIDATION", __EVENTVALIDATION2);
            NameValuePair p_61 = new BasicNameValuePair("ddlTipoConsulta", "1");
            NameValuePair p_71 = new BasicNameValuePair("txtNro", cel.getNumero());
            loginNvps2.add(p_11);
            loginNvps2.add(p_21);
            loginNvps2.add(p_31);
            loginNvps2.add(p_41);
            loginNvps2.add(p_51);
            loginNvps2.add(p_61);
            loginNvps2.add(p_71);
            /**
             *
             */

            HttpPost http3 = new HttpPost("http://200.60.223.134/vircanfiext/DirectorioRpm/frmDirectorioRpmRedi.aspx");
            http3.setEntity(new UrlEncodedFormEntity(loginNvps2, "UTF-8"));
            org.apache.http.HttpResponse resp3 = client.execute(http3);
            org.apache.http.HttpEntity entity3 = resp3.getEntity();
            InputStream instream3;
            instream3 = entity3.getContent();
            String rpta3 = IOUtils.toString(instream3, "UTF-8");

            rpta3 = rpta3.substring(rpta3.indexOf("pnlMensaje") + 10, rpta3.length());
            rpta3 = rpta3.substring(rpta3.indexOf(">") + 1, rpta3.length());
            rpta3 = rpta3.substring(0, rpta3.indexOf("<"));
//            extra();
//            System.out.println("rpta2:\n" + rpta3);
            if (rpta3.indexOf("no tiene") > -1) {
                return "";
            } else {
                rpta3 = rpta3.replaceAll("Número", "");
                rpta3 = rpta3.replaceAll("RPM", "");
                return rpta3.trim();
            }
            //cel.setNumeroRPM(rpta3.trim());
        } catch (Exception ep) {
            System.out.println(">" + ep.getMessage());
            return "";
        }

    }
}
